package org.jeecg.glr.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.jeecg.glr.entity.GlrLightPoleDevice;

import java.util.List;

/**
 * @Description: 路灯灯杆管绑定的设备
 * @Author: jeecg-boot
 * @Date:   2020-03-05
 * @Version: V1.0
 */
public interface GlrLightPoleDeviceMapper extends BaseMapper<GlrLightPoleDevice> {

    /**
     * 根据灯杆CODE和项目CODE查询灯杆设备列表
     * @param light_pole_code
     * @param project_code
     * @return
     */
    @Select("SELECT * from glr_light_pole_device WHERE light_pole_code=#{light_pole_code} and project_code=#{project_code}")
    List<GlrLightPoleDevice> selectGlrLightPoleDeviceListById(@Param("light_pole_code")String  light_pole_code, @Param("project_code")String  project_code);

    /**
     * 根据灯杆CODE、项目CODE、设备类型、设备ID查询灯杆设备列表
     * @param light_pole_code
     * @param project_code
     * @param device_type
     * @param device_id
     * @return
     */
    @Select({"<script>",
            " SELECT * FROM glr_light_pole_device",
            " WHERE 1=1",
            " AND light_pole_code=#{light_pole_code}",
            " AND project_code=#{project_code}",
            " AND device_type=#{device_type}",
            "<when test='device_id!=null and device_id!=\"\"'>",
            " AND device_id  &lt;&gt;   #{device_id}",
            "</when>",
            "</script>"})
    List<GlrLightPoleDevice> selectGlrLightPoleDeviceListByFind(
            @Param("light_pole_code")String  light_pole_code,
            @Param("project_code")String  project_code,
            @Param("device_type")String  device_type,
            @Param("device_id")String  device_id);

    /**
     * 根据设备ID和设备类型删除灯杆绑定的设备
     * @param device_id
     * @param device_type
     */
    @Delete("DELETE from glr_light_pole_device where device_id=#{device_id} and device_type=#{device_type} ")
    void deleteByDeviceIdAndDeviceType(@Param("device_id") String device_id, @Param("device_type") String device_type);

    /**
     * 根据设备ID清空设备绑定的灯杆CODE
     * @param update_table
     * @param device_id
     */
    @Update("update ${update_table}  set light_pole_code='' where id=#{device_id}")
    void updateTableLightPoleCodeById(@Param("update_table") String update_table, @Param("device_id") String device_id);

    /**
     * 根据网关代码删除灯杆关联设备
     * @param code
     */
    @Delete("delete from glr_light_pole_device where device_type='03' and device_id in (select id from glr_light where gate_way_code=#{gateWayCode})")
    void deleteByGateWayCode(@Param("gateWayCode")String  gateWayCode);

    /**
     * 根据网关代码查询需要更新状态的灯杆列表
     * @param gateWayCode
     * @return
     */
    @Select("select DISTINCT light_pole_code,project_code from glr_light_pole_device where device_type='03' and device_id in (select id from glr_light where gate_way_code=#{gateWayCode})")
    List<GlrLightPoleDevice> selectGlrLightPoleDeviceListByGateWayCode(@Param("gateWayCode")String  gateWayCode);
}
